package com.binghuojxj.utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import sun.java2d.cmm.CMSManager;

import java.io.IOException;
import java.io.InputStream;

/**
 * MyBatis 工具类
 * SqlSessionFactory -> sqlSession
 *
 * @author jxj
 * @date 2020-03-01
 **/
public class MyBatisUtils {
    private static SqlSessionFactory sqlSessionFactory;
    static {
        try {
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取 sqlSession
     * sqlSession 包含了面向数据库执行的所有方法
     */
    public static SqlSession getSqlSession(){
        return sqlSessionFactory.openSession();
    }
}
